package com.orangelife.redis.core.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import com.orangelife.utils.ProConfig;

public class RedisPool {
	private JedisPool jedisPool;// 非切片连接池
	private ShardedJedis shardedJedis;// 切片额客户端连接
	private ShardedJedisPool shardedJedisPool;// 切片连接池
	private int maxIdle = Integer.parseInt(ProConfig.getProValues("maxIdle"));
	private static String host = ProConfig.getProValues("redisHost");
	private static int port = Integer.parseInt(ProConfig
			.getProValues("redisPort"));
	private int MaxTotal = Integer.parseInt(ProConfig.getProValues("MaxTotal"));

	private int expireSeconds = 60 * 1;
	private long maxWaitMillis = Long.parseLong(ProConfig
			.getProValues("maxWaitMillis"));
	private boolean TestOnBorrow = false;

	// private static RedisPool redisPool=null;
	private static class RedisLazyHolder {

		// ProConfig.getProValue("redisHost")
		// ;Integer.parseInt(ProConfig.getProValue("redisPort"));
		// redisHost=127.0.0.1
		// redisPort=6379

		private static final RedisPool INSTANCE = new RedisPool();

	}

	//
	// static {
	//
	// //
	// System.out.println(ProConfig.getProValue("redisHost")+"*********************");
	// //
	// System.out.println(ProConfig.getProValue("redisPort")+"*********************");
	// //
	// // host=ProConfig.getProValue("redisHost") ;
	// // port=Integer.parseInt(ProConfig.getProValue("redisPort"));
	//
	// redisPool=new RedisPool();
	//
	// }

	public static RedisPool redisFactory() {

		// ProConfig.getProValue(key)

		return RedisLazyHolder.INSTANCE;
	}

	private RedisPool() {
		initialPool();

	}

	public void returnResource(Jedis jedis) {

		if (jedis != null) {
			jedis.close();
		}
	}

	/**
	 * 初始化非切片池
	 */
	private void initialPool() {
		// 池基本配置
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(this.maxIdle);
		config.setMaxWaitMillis(this.maxWaitMillis);
		config.setTestOnBorrow(this.TestOnBorrow);
		config.setMaxTotal(this.MaxTotal);
		jedisPool = new JedisPool(config, this.host, this.port);

	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	private void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	private ShardedJedis getShardedJedis() {
		return shardedJedis;
	}

	private void setShardedJedis(ShardedJedis shardedJedis) {
		this.shardedJedis = shardedJedis;
	}

	private ShardedJedisPool getShardedJedisPool() {
		return shardedJedisPool;
	}

	private void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}

	private String getHost() {
		return host;
	}

	private void setHost(String host) {
		this.host = host;
	}

	private int getPort() {
		return port;
	}

	private void setPort(int port) {
		this.port = port;
	}

	private long getMaxWaitMillis() {
		return maxWaitMillis;
	}

	private void setMaxWaitMillis(long maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}

	private boolean isTestOnBorrow() {
		return TestOnBorrow;
	}

	private void setTestOnBorrow(boolean testOnBorrow) {
		TestOnBorrow = testOnBorrow;
	}

	private int getExpireSeconds() {
		return expireSeconds;
	}

	private void setExpireSeconds(int expireSeconds) {
		this.expireSeconds = expireSeconds;
	}
}